const main = (allTree, part) => {
  // 扁平tree
  const allMap = new Map()
  while (allTree.length) { // 新push的children也会一直加入while循环
    const { children, ...item } = allTree.shift()
    allMap.set(item.id, item)
    children && allTree.push(...children)
  }
  // end
  // 把part中关联的父级加入
  let resList = []
  const addmark = {}
  part.forEach(item => {
    while (item) {
      !addmark[item.id] && resList.push(item) && (addmark[item.id] = 1)
      item = allMap.get(item.parentId)
    }
  })
  // 把resList组装成🌲
  const holds = [{
    id: null,
    children: []
  }, {
    id: "",
    children: []
  }]
  let res = [...holds]
  while (holds.length) {
    const p = holds.shift()
    p.children = []
    const pid = p.id
    let i = resList.length
    while (i--) {
      const item = resList[i]
      if (item.parentId === pid) {
        const { internalOrExternal, keepAlive, title, name = '', route, url, ...it } = item
        p.children.push({ ...it, path: url, route: route ? 1 : 0, name: url.replace(/^\//, '').replaceAll('/', '-'), meta: { internalOrExternal, keepAlive, title: title || name } })
        resList.splice(i, 1)
      }
    }
    p.children.sort((a, b) => a.sortNo - b.sortNo)
    holds.push(...p.children.filter(it => !it.leaf))
  }

  // 写入 JSON 数据
  res = [...res[0].children, ...res[1].children].sort((a, b) => a.sortNo - b.sortNo)
  // writeJSON({ res })
  return res
}

const allTree = [
  
]
const menuList = []

main(allTree, menuList)